package cn.com.HiSting1990.MyList;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

public class myarraylist {
    public static void main(String[] args) {
        //测试提交到代码
        //继续测试
        List<String> list = new ArrayList<String>();
        list.add("AAA");
        list.add("BBB");
        Iterator<String> iterator = list.iterator();
        HashMap<String, String> hashMap = new HashMap<>();

        for (String str:list) {
            System.out.println(str);
        }
        int a = 10;
        a = a >> 1;
        System.out.println(a);
        System.out.println("---------------");
        myarraylists();
    }

    //测试arraylist的容量和扩容机制
    public static void myarraylists(){
        List lists = new ArrayList<Integer>();

        for (int i=0;i<11;i++){
            lists.add(i);
        }
        System.out.println("大小"+lists.size());
        System.out.println("容量"+getArrayListCapacity(lists));
        System.out.println("------------");
        ArrayList<String> strings = new ArrayList<>(100);
        strings.add("A");
        System.out.println("大小"+strings.size());
        System.out.println("容量"+getArrayListCapacity(strings));

    }

    //查询arraylist的容量大小
    private static Integer getArrayListCapacity(List list){

        Integer length = null;

        Class listClass = list.getClass();
        Field elementData = null;
        try {
            elementData = listClass.getDeclaredField("elementData");
            elementData.setAccessible(true);
            Object[] o = (Object[]) elementData.get(list);
            length  = o.length;
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return length;
    }
}
